﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.Ptoduction;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Read.API.Application.Commands.SCWorkCommand;
using SmartProduction.Read.API.Dto;

namespace SmartProduction.Read.API.Application.CommandHandlers.SCWorkCommandHandler
{
    public class SCWorkOrderQueryCommandHandler:IRequestHandler<SCWorkOrderQueryCommand,APIPaing<List<SCWorkOrderDtos>>>
    {
        private readonly IMapper mapper;
        /// <summary>
        /// 基本信息仓储接口
        /// </summary>
        private readonly IBaseRepository<ProductionWorkOrderBasicInfoSheet> jRepo;
        /// <summary>
        /// 物品仓储接口
        /// </summary>
        private readonly IBaseRepository<ProductionItem> wRepo;
        /// <summary>
        /// 物品名称仓储接口
        /// </summary>
        private readonly IBaseRepository<item_classification_detail> wnRepo;


        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="jRepo"></param>
        /// <param name="wRepo"></param>
        /// <param name="wnRepo"></param>
        /// <param name="mapper"></param>
        public SCWorkOrderQueryCommandHandler(IMapper mapper, IBaseRepository<ProductionWorkOrderBasicInfoSheet> jRepo, IBaseRepository<ProductionItem> wRepo, IBaseRepository<item_classification_detail> wnRepo)
        {
            this.mapper = mapper;
            this.jRepo = jRepo;
            this.wRepo = wRepo;
            this.wnRepo = wnRepo;
        }


        public async Task<APIPaing<List<SCWorkOrderDtos>>> Handle(SCWorkOrderQueryCommand request, CancellationToken cancellationToken)
        {
            APIPaing<List<SCWorkOrderDtos>> result = new APIPaing<List<SCWorkOrderDtos>>();

            var list = from a in jRepo.GetAll().Result
                       join b in wRepo.GetAll().Result on a.ProductionItemId equals b.ProductionItemId
                       join c in wnRepo.GetAll().Result on b.itemName equals c.item_classification_detailID
                       select new SCWorkOrderDtos
                       {
                           WorkOrderId = a.WorkOrderId,
                           WorkOrderName = a.WorkOrderName,
                           WorkOrderNumber = a.WorkOrderNumber,
                           item_name = c.item_name,
                           PersoninCharge = a.PersoninCharge,
                           AffiliatedDepartment = a.AffiliatedDepartment,
                           plannedOutput = b.plannedOutput,
                           IsDel = a.IsDel
                       };
            if (!string.IsNullOrEmpty(request.WorkOrderName))
            {
                list = list.Where(x => x.WorkOrderName.Contains(request.WorkOrderName));
            }

            result.TotalCount = list.Count();
            result.PageCount = (int)Math.Ceiling((double)result.TotalCount / request.PageSize);
            list = list.OrderByDescending(o => o.WorkOrderId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            var data = mapper.Map<List<SCWorkOrderDtos>>(list);
            result.Code = APIEnums.查询成功;
            result.Msg = APIEnums.查询成功.ToString();
            result.data = data;
            return await Task.FromResult(result);
        }
    }
}
